﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class SupplierController : SecureController
    {
        private readonly IItemCatDao _ItemCatDao;
        private readonly ISupplierDao _SupplierDao;
        private readonly ICommonDao<Supplier> _CommonDao;
        public SupplierController(IItemCatDao itemcatdao, ISupplierDao supplierDao,ICommonDao<Supplier> commonDao)
        {
            _SupplierDao = supplierDao;
            _ItemCatDao = itemcatdao;
            _CommonDao = commonDao;
        }
        
        public ActionResult Index()
        {

            return View(_CommonDao.GetAll());
        }
        public ActionResult Create()
        {
           
            return View(new Supplier());
        }

        [HttpPost]
        public ActionResult Create(Supplier supplier)
        {
            
            if (ModelState.IsValid)
            {
                _CommonDao.Save(supplier);
                _CommonDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(supplier);
        }

        public ActionResult Edit(int id)
        {

            return View(_CommonDao.GetById(id));
        }


        public ActionResult Delete(int id)
        {
            var brand = _SupplierDao.GetById(id);
            _SupplierDao.Delete(brand);
            _SupplierDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, Supplier supplier)
        { 
            if (ModelState.IsValid)
            {
                var dbsupplier = _CommonDao.GetById(id);
                UpdateModel<Supplier>(dbsupplier, new string[] { "Name", "LinkMan", "Tel", "Mobile", "Address", "Memo" });
                _CommonDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(supplier);

            
        }

    }
}
